<template>
  <a-form
    :model="formState"
    ref="formDataRef"
    name="basic"
    :label-col="{ style: 'width:80px' }"
    :wrapper-col="{ style: 'calc(100% - 80px)' }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="国家" name="countryRegionId" :rules="[{ required: true, message: '请填写件数!' }]">
      <a-select v-model:value="formState.countryRegionId" placeholder="请选择" @change="changeCountryHandle">
        <a-select-option v-for="(item, index) in countryOpts" :key="index" :value="item.id">{{ item.region }}</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="渠道" name="channelId" :rules="[{ required: true, message: '请选择渠道!' }]">
      <a-cascader v-model:value="formState.channelId" showSearch :options="channelOpts" placeholder="请选择" />
    </a-form-item>
    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button :loading="loading" type="primary" html-type="submit">确定</a-button>
      <a-button @click="resetHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { toRefs, watch } from "vue";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: StockManagement.ListRows;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "submit"): void;
}>();
const { visible, passData } = toRefs(props);
const { formDataRef, formState, loading, countryOpts, getAllCountryOpts, channelOpts, getChannelOpts } = InitHook({ passData });
const { onFinish, onFinishFailed, resetFields, resetHandle, changeCountryHandle } = OperationHook({
  formDataRef,
  formState,
  passData,
  emit,
  loading,
  getChannelOpts
});
const init = () => {
  formState.countryRegionId = passData.value.countryRegionId;
  formState.channelId = [passData.value.channelAllocation, passData.value.channelId];
  getAllCountryOpts();
  getChannelOpts();
};
watch(
  () => visible.value,
  (v: boolean) => {
    if (!v) {
      resetFields();
      formDataRef.value.clearValidate();
    } else {
      init();
    }
  }
);
init();
</script>
<style lang="stylus" scoped>
:deep(.ant-picker){
    width: 100%;
}
</style>
